package com.skyworth.web.admin.menu.service.impl;

import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.skyworth.utils.Chn2Py;
import com.skyworth.web.admin.menu.domain.MenuModel;
import com.skyworth.web.admin.menu.form.MenuForm;
import com.skyworth.web.admin.menu.repository.MenuRepository;
import com.skyworth.web.admin.menu.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * Created by skyworth on 2016/8/8.
 */
@Service
public class MenuServiceImpl implements MenuService {
	Logger LOG = LoggerFactory.getLogger(MenuServiceImpl.class);

	@Autowired
	private MenuRepository menuRepository;

	@PersistenceContext
	private EntityManager em;

	@Override
	public MenuModel addMenu(MenuForm menuForm) {
		MenuModel menuModel = new MenuModel();
		menuModel.name = menuForm.name;
		menuModel.categoryId = menuForm.categoryId;
		menuModel.firstLetter = Chn2Py.chn2py(menuForm.name);
		if(null != menuForm.unitPrice && !menuForm.unitPrice.isEmpty()) {
			menuModel.unitPrice = Float.parseFloat(menuForm.unitPrice);
		}else {
			menuModel.unitPrice = 0;
		}
		if(null != menuForm.unitName && !menuForm.unitName.isEmpty()) {
			menuModel.unitName = menuForm.unitName;
		}else {
			menuModel.unitName = "份";
		}
		if(null != menuForm.discount && !menuForm.discount.isEmpty()) {
			menuModel.discount = Float.parseFloat(menuForm.discount);
		}else {
			menuModel.discount = 10;
		}

		menuModel.recommend = menuForm.recommend;
		menuModel.stopSale = menuForm.stopSale;
		menuModel.imgId = menuForm.imgId;
		menuModel.storeId = menuForm.storeId;
		menuRepository.save(menuModel);

		return menuModel;
	}

	@Override
	public void delMenu(long id) {
		menuRepository.logicDel(id);
//		menuRepository.delete(id);
	}

	@Override
	public MenuModel updateModel(MenuForm menuForm) {
		MenuModel menuModel = menuRepository.findOne(menuForm.id);
		menuModel.name = menuForm.name;
		menuModel.firstLetter = Chn2Py.chn2py(menuForm.name);
		if(menuForm.categoryId != menuModel.categoryId) {
			menuModel.categoryId = menuForm.categoryId;
		}
		if(null != menuForm.unitPrice && !menuForm.unitPrice.isEmpty()) {
			menuModel.unitPrice = Float.parseFloat(menuForm.unitPrice);
		}else {
			menuModel.unitPrice = 0;
		}
		if(null != menuForm.discount && !menuForm.discount.isEmpty()){
			menuModel.discount = Float.parseFloat(menuForm.discount);
		}else{
			menuModel.discount = 10;
		}

		if(null != menuForm.unitName && !menuForm.unitName.isEmpty()) {
			menuModel.unitName = menuForm.unitName;
		}else {
			menuModel.unitName = "份";
		}

		menuModel.recommend = menuForm.recommend;
		menuModel.stopSale = menuForm.stopSale;
		if(null != menuForm.discount && !menuForm.discount.isEmpty()) {
			menuModel.discount = Float.parseFloat(menuForm.discount);
		}else {
			menuModel.discount = 0;
		}
		menuModel.imgId = menuForm.imgId;
		menuRepository.save(menuModel);
		return menuModel;
	}

	@Override
	public boolean existMenu(String name,long store_id) {
		return menuRepository.existByName(name, store_id);
	}

	@Override
	public int soldOutToggle(long menuId, int flag) {
		return menuRepository.updateSoldOut(menuId, flag);
	}

	@Override
	public MenuModel findMenu(long id) {
		return menuRepository.findOne(id);
	}

	@Override
	public List<MenuModel> listMenu(JsonObject params) {

		return Lists.newArrayList(menuRepository.findAll());
	}

	@Override
	public List<MenuModel> listMenuByStore(long storeId) {
		return menuRepository.findByStore(storeId);
	}

	@Override
	public Page<MenuModel> pageMenu(JsonObject params, Pageable pageable) {
		long store_id = 0;
		if(null != params){
			store_id = params.get("store_id").getAsLong();
		}
		if(params != null && null != params.get("category_id")){
			String categoryIdStr = params.get("category_id").getAsString();
			if(categoryIdStr != null && !categoryIdStr.isEmpty()){
				Long categoryId = Long.parseLong(categoryIdStr);
				return menuRepository.findByCategoryId(categoryId,store_id,pageable);
			}
		}
		return menuRepository.allValidMenu(store_id,pageable);
	}

	@Override
	public List<MenuModel> getByCategoryId(long id) {
		return menuRepository.findByCategoryId(id);
	}

	@Override
	public List<MenuModel> queryMenu(Long storeId, String key_word) {
		return  menuRepository.queryByParams(storeId,key_word);
	}

	@Override
	public boolean existMenu(String name, long id, long store_id) {
		return menuRepository.existMenu(name, id, store_id);
	}
}
